/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.dao.RoleDAO;
import model.dao.UserDAO;
import model.dao.services.RoleDAOServices;
import model.dao.services.UserDAOServices;
import model.entities.Role;
import model.entities.User;

/**
 *
 * @author Ku81
 */
public class RegistrationServlet extends HttpServlet {
    
    private final RoleDAOServices ROLE_DAO = RoleDAO.getInstance();
    private final UserDAOServices USER_DAO = UserDAO.getInstance();

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    protected void doRegister(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{
        response.setContentType("text/html;charset=UTF-8");
        String userName = request.getParameter("");
        String password = request.getParameter("");
        String fullName = request.getParameter("");
        String dateStr = "";
        boolean gender = request.getParameter("").equals("M")?true:false;
        String homeTown = request.getParameter("");
        String school =request.getParameter("");
        String phone =request.getParameter("");
        String email = request.getParameter("");
        String strenght = request.getParameter("");
        String language = request.getParameter("");
        String image = request.getParameter("");
        String about = request.getParameter("");
        String favoriteQuotations = request.getParameter("");
        String facebookLink = request.getParameter("");
        String activeCode = util.Random.randomString(9);
        Role role = ROLE_DAO.getById(2);
        if(!USER_DAO.isExistUserName(userName)){
            User u = new User(userName, password, fullName, null, 
                    gender, homeTown, school, phone, email, strenght, language,
                    image, about, favoriteQuotations, facebookLink, activeCode, role, gender);
            if(USER_DAO.create(u)){
                util.MailSender.sendMail(userName, email, activeCode);
                //Success page
            }else{
                //Failed page
            }
        }
    }
}
